import os
import sys

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QFileDialog, QMessageBox, QWidget

from ..config import ExcelConfig, R
from ..core.excel import ExcelAccount, ExcelFillColumn, ExcelFree, ExcelSale, ExcelTask
from .gen_frees_ui import Ui_Form


class GenerateFreesWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        self.ui.pushButton_SelectOrgFile.clicked.connect(self.SelectOrgFile)
        self.ui.pushButton_SelectTempFile.clicked.connect(self.SelectTempFile)
        self.ui.pushButton_GenerateFrees.clicked.connect(self.GenerateFrees)
        self.ui.pushButton_GenerateTasks.clicked.connect(self.GenerateTasks)
        self.InitUi()
        self.filename = "."

    def InitUi(self):
        self.ui.lineEdit_OrgFilename.setText(R.R_OrgFilename)
        self.ui.lineEdit_TempFilename.setText(R.R_TempFilename)
        self.ui.lineEdit_TempFilename.setText(R.R_TempFilename)
        self.ui.lineEdit_TempFilename.setText(R.R_TempFilename)
        self.ui.lineEdit_OrgSheet1Name.setText(R.R_OrgSheet1Name)
        self.ui.lineEdit_OrgSheet1StartRow.setText(R.R_OrgSheet1StartRow)
        self.ui.lineEdit_OrgSheet1EndRow.setText(R.R_OrgSheet1EndRow)
        self.ui.lineEdit_OrgSheet1StartCol.setText(R.R_OrgSheet1StartCol)
        self.ui.lineEdit_OrgSheet1EndCol.setText(R.R_OrgSheet1EndCol)
        self.ui.lineEdit_OrgSheet1ClientRow.setText(R.R_OrgSheet1ClientRow)
        self.ui.lineEdit_OrgSheet1CodeCol.setText(R.R_OrgSheet1CodeCol)
        self.ui.lineEdit_OrgSheet2Name.setText(R.R_OrgSheet2Name)
        self.ui.lineEdit_OrgSheet2StartRow.setText(R.R_OrgSheet2StartRow)
        self.ui.lineEdit_OrgSheet2EndRow.setText(R.R_OrgSheet2EndRow)
        self.ui.lineEdit_OrgSheet2StartCol.setText(R.R_OrgSheet2StartCol)
        self.ui.lineEdit_OrgSheet2EndCol.setText(R.R_OrgSheet2EndCol)
        self.ui.lineEdit_OrgSheet2ClientRow.setText(R.R_OrgSheet2ClientRow)
        self.ui.lineEdit_OrgSheet2CodeCol.setText(R.R_OrgSheet2CodeCol)
        self.ui.lineEdit_TempSheet0Name.setText(R.R_TempSheet0Name)
        self.ui.lineEdit_TempSheet0ClientRow.setText(R.R_TempSheet0ClientRow)
        self.ui.lineEdit_TempSheet0ClientCol.setText(R.R_TempSheet0ClientCol)
        self.ui.lineEdit_TempSheet1Name.setText(R.R_TempSheet1Name)
        self.ui.lineEdit_TempSheet1CodeCol.setText(R.R_TempSheet1CodeCol)
        self.ui.lineEdit_TempSheet1StartRow.setText(R.R_TempSheet1StartRow)
        self.ui.lineEdit_TempSheet1EndRow.setText(R.R_TempSheet1EndRow)
        self.ui.lineEdit_TempSheet1FillCol.setText(R.R_TempSheet1FillCol)
        self.ui.lineEdit_TempSheet2Name.setText(R.R_TempSheet2Name)
        self.ui.lineEdit_TempSheet2CodeCol.setText(R.R_TempSheet2CodeCol)
        self.ui.lineEdit_TempSheet2StartRow.setText(R.R_TempSheet2StartRow)
        self.ui.lineEdit_TempSheet2EndRow.setText(R.R_TempSheet2EndRow)
        self.ui.lineEdit_TempSheet2FillCol.setText(R.R_TempSheet2FillCol)
        self.ui.lineEdit_TempSheet3Name.setText(R.R_TempSheet3Name)
        self.ui.lineEdit_TempSheet3CodeCol.setText(R.R_TempSheet3CodeCol)
        self.ui.lineEdit_TempSheet3StartRow.setText(R.R_TempSheet3StartRow)
        self.ui.lineEdit_TempSheet3EndRow.setText(R.R_TempSheet3EndRow)
        self.ui.lineEdit_TempSheet3FillCol.setText(R.R_TempSheet3FillCol)
        self.ui.lineEdit_TempSheet4Name.setText(R.R_TempSheet4Name)
        self.ui.lineEdit_TempSheet4CodeCol.setText(R.R_TempSheet4CodeCol)
        self.ui.lineEdit_TempSheet4StartRow.setText(R.R_TempSheet4StartRow)
        self.ui.lineEdit_TempSheet4EndRow.setText(R.R_TempSheet4EndRow)
        self.ui.lineEdit_TempSheet4FillCol.setText(R.R_TempSheet4FillCol)
        self.ui.lineEdit_TempSheet5Name.setText(R.R_TempSheet5Name)
        self.ui.lineEdit_TempSheet5CodeCol.setText(R.R_TempSheet5CodeCol)
        self.ui.lineEdit_TempSheet5StartRow.setText(R.R_TempSheet5StartRow)
        self.ui.lineEdit_TempSheet5EndRow.setText(R.R_TempSheet5EndRow)
        self.ui.lineEdit_TempSheet5FillCol.setText(R.R_TempSheet5FillCol)
        self.ui.lineEdit_TempSheet6Name.setText(R.R_TempSheet6Name)
        self.ui.lineEdit_TempSheet6CodeCol.setText(R.R_TempSheet6CodeCol)
        self.ui.lineEdit_TempSheet6StartRow.setText(R.R_TempSheet6StartRow)
        self.ui.lineEdit_TempSheet6EndRow.setText(R.R_TempSheet6EndRow)
        self.ui.lineEdit_TempSheet6FillCol.setText(R.R_TempSheet6FillCol)

    def SelectOrgFile(self):
        filename, ext = QFileDialog.getOpenFileName(
            self,
            "OpenFile",
            os.path.dirname(self.filename),
            "Excel File (*.xlsx);; All File (*.*)",
        )
        if filename:
            self.ui.lineEdit_OrgFilename.setText(filename)
            self.filename = filename
        else:
            QMessageBox.about(self, "Open file", "No opened file!")

    def SelectTempFile(self):
        filename, ext = QFileDialog.getOpenFileName(
            self,
            "OpenFile",
            os.path.dirname(self.filename),
            "Excel File (*.xlsx);; All File (*.*)",
        )
        if filename:
            self.ui.lineEdit_TempFilename.setText(filename)
            self.filename = filename
        else:
            QMessageBox.about(self, "Open file", "No opened file!")

    def GenerateFrees(self):
        try:
            OrgFilename = self.ui.lineEdit_OrgFilename.text()
            TempFilename = self.ui.lineEdit_TempFilename.text()
            OrgSheet1Name = self.ui.lineEdit_OrgSheet1Name.text()
            OrgSheet1StartRow = R.toInt(self.ui.lineEdit_OrgSheet1StartRow.text())
            OrgSheet1EndRow = R.toInt(self.ui.lineEdit_OrgSheet1EndRow.text())
            OrgSheet1StartCol = R.toInt(self.ui.lineEdit_OrgSheet1StartCol.text())
            OrgSheet1EndCol = R.toInt(self.ui.lineEdit_OrgSheet1EndCol.text())
            OrgSheet1ClientRow = R.toInt(self.ui.lineEdit_OrgSheet1ClientRow.text())
            OrgSheet1CodeCol = R.toInt(self.ui.lineEdit_OrgSheet1CodeCol.text())
            OrgSheet2Name = self.ui.lineEdit_OrgSheet2Name.text()
            OrgSheet2StartRow = R.toInt(self.ui.lineEdit_OrgSheet2StartRow.text())
            OrgSheet2EndRow = R.toInt(self.ui.lineEdit_OrgSheet2EndRow.text())
            OrgSheet2StartCol = R.toInt(self.ui.lineEdit_OrgSheet2StartCol.text())
            OrgSheet2EndCol = R.toInt(self.ui.lineEdit_OrgSheet2EndCol.text())
            OrgSheet2ClientRow = R.toInt(self.ui.lineEdit_OrgSheet2ClientRow.text())
            OrgSheet2CodeCol = R.toInt(self.ui.lineEdit_OrgSheet2CodeCol.text())
            TempSheet0Name = self.ui.lineEdit_TempSheet0Name.text()
            TempSheet0ClientRow = R.toInt(self.ui.lineEdit_TempSheet0ClientRow.text())
            TempSheet0ClientCol = R.toInt(self.ui.lineEdit_TempSheet0ClientCol.text())
            TempSheet1Name = self.ui.lineEdit_TempSheet1Name.text()
            TempSheet1CodeCol = R.toInt(self.ui.lineEdit_TempSheet1CodeCol.text())
            TempSheet1StartRow = R.toInt(self.ui.lineEdit_TempSheet1StartRow.text())
            TempSheet1EndRow = R.toInt(self.ui.lineEdit_TempSheet1EndRow.text())
            TempSheet1FillCol = R.toInt(self.ui.lineEdit_TempSheet1FillCol.text())
            TempSheet2Name = self.ui.lineEdit_TempSheet2Name.text()
            TempSheet2CodeCol = R.toInt(self.ui.lineEdit_TempSheet2CodeCol.text())
            TempSheet2StartRow = R.toInt(self.ui.lineEdit_TempSheet2StartRow.text())
            TempSheet2EndRow = R.toInt(self.ui.lineEdit_TempSheet2EndRow.text())
            TempSheet2FillCol = R.toInt(self.ui.lineEdit_TempSheet2FillCol.text())
            TempSheet3Name = self.ui.lineEdit_TempSheet3Name.text()
            TempSheet3CodeCol = R.toInt(self.ui.lineEdit_TempSheet3CodeCol.text())
            TempSheet3StartRow = R.toInt(self.ui.lineEdit_TempSheet3StartRow.text())
            TempSheet3EndRow = R.toInt(self.ui.lineEdit_TempSheet3EndRow.text())
            TempSheet3FillCol = R.toInt(self.ui.lineEdit_TempSheet3FillCol.text())
            TempSheet4Name = self.ui.lineEdit_TempSheet4Name.text()
            TempSheet4CodeCol = R.toInt(self.ui.lineEdit_TempSheet4CodeCol.text())
            TempSheet4StartRow = R.toInt(self.ui.lineEdit_TempSheet4StartRow.text())
            TempSheet4EndRow = R.toInt(self.ui.lineEdit_TempSheet4EndRow.text())
            TempSheet4FillCol = R.toInt(self.ui.lineEdit_TempSheet4FillCol.text())
            TempSheet5Name = self.ui.lineEdit_TempSheet5Name.text()
            TempSheet5CodeCol = R.toInt(self.ui.lineEdit_TempSheet5CodeCol.text())
            TempSheet5StartRow = R.toInt(self.ui.lineEdit_TempSheet5StartRow.text())
            TempSheet5EndRow = R.toInt(self.ui.lineEdit_TempSheet5EndRow.text())
            TempSheet5FillCol = R.toInt(self.ui.lineEdit_TempSheet5FillCol.text())
            TempSheet6Name = self.ui.lineEdit_TempSheet6Name.text()
            TempSheet6CodeCol = R.toInt(self.ui.lineEdit_TempSheet6CodeCol.text())
            TempSheet6StartRow = R.toInt(self.ui.lineEdit_TempSheet6StartRow.text())
            TempSheet6EndRow = R.toInt(self.ui.lineEdit_TempSheet6EndRow.text())
            TempSheet6FillCol = R.toInt(self.ui.lineEdit_TempSheet6FillCol.text())

            """ 生成免费报表 """
            org_file = OrgFilename
            temp_file = TempFilename

            exl_free = ExcelFree(temp_file)
            exl_free.open(org_file)
            exl_free.init_models(
                org_sheet1_name=OrgSheet1Name,
                org_sheet1_start_row=OrgSheet1StartRow,
                org_sheet1_end_row=OrgSheet1EndRow,
                org_sheet1_start_col=OrgSheet1StartCol,
                org_sheet1_end_col=OrgSheet1EndCol,
                org_sheet1_client_row=OrgSheet1ClientRow,
                org_sheet1_code_col=OrgSheet1CodeCol,
                org_sheet2_name=OrgSheet2Name,
                org_sheet2_start_row=OrgSheet2StartRow,
                org_sheet2_end_row=OrgSheet2EndRow,
                org_sheet2_start_col=OrgSheet2StartCol,
                org_sheet2_end_col=OrgSheet2EndCol,
                org_sheet2_client_row=OrgSheet2ClientRow,
                org_sheet2_code_col=OrgSheet2CodeCol,
                temp_sheet0_name=TempSheet0Name,
                temp_sheet0_client_row=TempSheet0ClientRow,
                temp_sheet0_client_col=TempSheet0ClientCol,
                temp_sheet1_name=TempSheet1Name,
                tmep_sheet1_code_col=TempSheet1CodeCol,
                temp_sheet1_start_row=TempSheet1StartRow,
                temp_sheet1_end_row=TempSheet1EndRow,
                temp_sheet1_fill_col=TempSheet1FillCol,
                temp_sheet2_name=TempSheet2Name,
                tmep_sheet2_code_col=TempSheet2CodeCol,
                temp_sheet2_start_row=TempSheet2StartRow,
                temp_sheet2_end_row=TempSheet2EndRow,
                temp_sheet2_fill_col=TempSheet2FillCol,
                temp_sheet3_name=TempSheet3Name,
                tmep_sheet3_code_col=TempSheet3CodeCol,
                temp_sheet3_start_row=TempSheet3StartRow,
                temp_sheet3_end_row=TempSheet3EndRow,
                temp_sheet3_fill_col=TempSheet3FillCol,
                temp_sheet4_name=TempSheet4Name,
                tmep_sheet4_code_col=TempSheet4CodeCol,
                temp_sheet4_start_row=TempSheet4StartRow,
                temp_sheet4_end_row=TempSheet4EndRow,
                temp_sheet4_fill_col=TempSheet4FillCol,
                temp_sheet5_name=TempSheet5Name,
                tmep_sheet5_code_col=TempSheet5CodeCol,
                temp_sheet5_start_row=TempSheet5StartRow,
                temp_sheet5_end_row=TempSheet5EndRow,
                temp_sheet5_fill_col=TempSheet5FillCol,
                temp_sheet6_name=TempSheet6Name,
                tmep_sheet6_code_col=TempSheet6CodeCol,
                temp_sheet6_start_row=TempSheet6StartRow,
                temp_sheet6_end_row=TempSheet6EndRow,
                temp_sheet6_fill_col=TempSheet6FillCol,
            )
            exl_free.generate_free()

            QMessageBox.about(self, R.AppName, "生成免费申报表成功！")

        except Exception as e:
            QMessageBox.about(self, R.AppName, f"请填充正确的数字！Errors: {e}")

    def GenerateTasks(self):
        try:
            OrgFilename = self.ui.lineEdit_OrgFilename.text()
            TempFilename = self.ui.lineEdit_TempFilename.text()
            OrgSheet1Name = self.ui.lineEdit_OrgSheet1Name.text()
            OrgSheet1StartRow = R.toInt(self.ui.lineEdit_OrgSheet1StartRow.text())
            OrgSheet1EndRow = R.toInt(self.ui.lineEdit_OrgSheet1EndRow.text())
            OrgSheet1StartCol = R.toInt(self.ui.lineEdit_OrgSheet1StartCol.text())
            OrgSheet1EndCol = R.toInt(self.ui.lineEdit_OrgSheet1EndCol.text())
            OrgSheet1ClientRow = R.toInt(self.ui.lineEdit_OrgSheet1ClientRow.text())
            OrgSheet1CodeCol = R.toInt(self.ui.lineEdit_OrgSheet1CodeCol.text())
            OrgSheet2Name = self.ui.lineEdit_OrgSheet2Name.text()
            OrgSheet2StartRow = R.toInt(self.ui.lineEdit_OrgSheet2StartRow.text())
            OrgSheet2EndRow = R.toInt(self.ui.lineEdit_OrgSheet2EndRow.text())
            OrgSheet2StartCol = R.toInt(self.ui.lineEdit_OrgSheet2StartCol.text())
            OrgSheet2EndCol = R.toInt(self.ui.lineEdit_OrgSheet2EndCol.text())
            OrgSheet2ClientRow = R.toInt(self.ui.lineEdit_OrgSheet2ClientRow.text())
            OrgSheet2CodeCol = R.toInt(self.ui.lineEdit_OrgSheet2CodeCol.text())
            TempSheet0Name = self.ui.lineEdit_TempSheet0Name.text()
            TempSheet0ClientRow = R.toInt(self.ui.lineEdit_TempSheet0ClientRow.text())
            TempSheet0ClientCol = R.toInt(self.ui.lineEdit_TempSheet0ClientCol.text())
            TempSheet1Name = self.ui.lineEdit_TempSheet1Name.text()
            TempSheet1CodeCol = R.toInt(self.ui.lineEdit_TempSheet1CodeCol.text())
            TempSheet1StartRow = R.toInt(self.ui.lineEdit_TempSheet1StartRow.text())
            TempSheet1EndRow = R.toInt(self.ui.lineEdit_TempSheet1EndRow.text())
            TempSheet1FillCol = R.toInt(self.ui.lineEdit_TempSheet1FillCol.text())
            TempSheet2Name = self.ui.lineEdit_TempSheet2Name.text()
            TempSheet2CodeCol = R.toInt(self.ui.lineEdit_TempSheet2CodeCol.text())
            TempSheet2StartRow = R.toInt(self.ui.lineEdit_TempSheet2StartRow.text())
            TempSheet2EndRow = R.toInt(self.ui.lineEdit_TempSheet2EndRow.text())
            TempSheet2FillCol = R.toInt(self.ui.lineEdit_TempSheet2FillCol.text())
            TempSheet3Name = self.ui.lineEdit_TempSheet3Name.text()
            TempSheet3CodeCol = R.toInt(self.ui.lineEdit_TempSheet3CodeCol.text())
            TempSheet3StartRow = R.toInt(self.ui.lineEdit_TempSheet3StartRow.text())
            TempSheet3EndRow = R.toInt(self.ui.lineEdit_TempSheet3EndRow.text())
            TempSheet3FillCol = R.toInt(self.ui.lineEdit_TempSheet3FillCol.text())
            TempSheet4Name = self.ui.lineEdit_TempSheet4Name.text()
            TempSheet4CodeCol = R.toInt(self.ui.lineEdit_TempSheet4CodeCol.text())
            TempSheet4StartRow = R.toInt(self.ui.lineEdit_TempSheet4StartRow.text())
            TempSheet4EndRow = R.toInt(self.ui.lineEdit_TempSheet4EndRow.text())
            TempSheet4FillCol = R.toInt(self.ui.lineEdit_TempSheet4FillCol.text())
            TempSheet5Name = self.ui.lineEdit_TempSheet5Name.text()
            TempSheet5CodeCol = R.toInt(self.ui.lineEdit_TempSheet5CodeCol.text())
            TempSheet5StartRow = R.toInt(self.ui.lineEdit_TempSheet5StartRow.text())
            TempSheet5EndRow = R.toInt(self.ui.lineEdit_TempSheet5EndRow.text())
            TempSheet5FillCol = R.toInt(self.ui.lineEdit_TempSheet5FillCol.text())
            TempSheet6Name = self.ui.lineEdit_TempSheet6Name.text()
            TempSheet6CodeCol = R.toInt(self.ui.lineEdit_TempSheet6CodeCol.text())
            TempSheet6StartRow = R.toInt(self.ui.lineEdit_TempSheet6StartRow.text())
            TempSheet6EndRow = R.toInt(self.ui.lineEdit_TempSheet6EndRow.text())
            TempSheet6FillCol = R.toInt(self.ui.lineEdit_TempSheet6FillCol.text())

            """
            生成任务责任书，格式有变，需改代码
            """
            exl = ExcelTask(
                src_path=OrgFilename,
                src_sheetname=OrgSheet1Name,
                src_start_row=OrgSheet1StartRow,
                src_end_row=OrgSheet1EndRow,
                dest_path=TempFilename,
                dest_sheetname=TempSheet0Name,
            )
            exl.FillTask()

            QMessageBox.about(self, R.AppName, "生成任务表成功！")

        except Exception as e:
            QMessageBox.about(self, R.AppName, f"请填充正确的数字！Errors: {e}")
